#!/bin/bash
#
# Copyright 2015-2020 The OpenZipkin Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
#


set -v

# Docker Hub's default build command seems to not handle .dockerignore correctly. Doing a simple build command
# ourselves works fine.

# This hook is called with the current directory set to the same as the Dockerfile, so we go back
# to top level.
cd ..

echo "Building images for $SOURCE_BRANCH"

docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "$IMAGE_NAME" .

IFS=',' read -ra TAGS <<< "$DOCKER_TAG"
for tag in ${TAGS[@]}; do
  docker tag "$IMAGE_NAME" "${DOCKER_REPO}:${tag}"
done

# We always build zipkin-slim
echo Building zipkin-slim
docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "openzipkin/zipkin-slim:${TAGS[0]}" --target zipkin-slim .
for tag in ${TAGS[@]:1}; do
  docker tag "openzipkin/zipkin-slim:${TAGS[0]}" "openzipkin/zipkin-slim:$tag"
done

# We always build test images formerly hosted on https://github.com/openzipkin/docker-zipkin
echo Building zipkin-ui
docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "openzipkin/zipkin-ui:${TAGS[0]}" --target zipkin-ui .
for tag in ${TAGS[@]:1}; do
  docker tag "openzipkin/zipkin-ui:${TAGS[0]}" "openzipkin/zipkin-ui:$tag"
done

# We also build testing images to correspond with the server version to keep schemas up to date
for path in collector/kafka storage/cassandra storage/elasticsearch6 storage/elasticsearch7 storage/mysql; do
  image=zipkin-$(basename ${path})
  echo Building ${image}
  docker build --build-arg source_branch=${SOURCE_BRANCH} -f "docker/${path}/Dockerfile" -t "openzipkin/${image}:${TAGS[0]}" .
  for tag in ${TAGS[@]:1}; do
    docker tag "openzipkin/${image}:${TAGS[0]}" "openzipkin/${image}:$tag"
  done
done
